var uid = $.cookie("uid");
if (uid) {
	$.ajax({
		type: "get",
		url: "/shopping/shopping_list",
		dataType: "JSON",
		data: {
			uid: uid
		},
		success: function (data) {
			console.log(data.length > 0 ? 1 : 2);
			if (data.length > 0) {
				$("#shop1").removeClass("d-none");
				$("#shop2").addClass("d-none")
				$("#shop3").addClass("d-none");
				var str = "";
				for (var key in data) {
					$.ajax({
						type: "get",
						url: "/product/details",
						dataType: "JSON",
						async: false,
						data: {
							pid: data[key].pid
						},
						success: function (res) {
							console.log("========================");
							console.log(data);
							str +=
								`<tr><td class="zp-1"><div class=" d-flex justify-content-around align-items-center"><input type="checkbox" value="${data[key].shopid}"/><div><img  src="${res[0].pimg}" style="width: 90px;height: 90px;"></div><div class="text-left pl-2"><p><b>${res[0].title}</b></p><div class="zp-s1"><p>商品：NWTB18410100281</p><p>尺寸：${data[key].size}</p><p>颜色：${data[key].color}</p></div></div></div></td><td class="zp-2 font-weight-bold l-height-9">¥${res[0].price}</td><td class="zp-3"><div class="d-flex justify-content-around align-items-center"><button class="btn text-white bg-dark" type="button">-</button><input class="form-control w-25 text-center" type="text"  value="${data[key].count}"><button class="btn text-white" type="button">+</button></div></td><td class="zp-4 font-weight-bold l-height-9">¥${data[key].count*res[0].price}</td><td class="zp-5"><a href="javascript:;" class="font-weight-bold text-dark l-height-9 shop_delete">X</a></td></tr>`;
						}
					})
				}
				$("#zp_body").empty().append(str)
			} else {
				$("#shop1").addClass("d-none");
				$("#shop2").removeClass("d-none")
				$("#shop3").addClass("d-none");
			}

			$(".shop_delete").click(function () {
				var shopid = $(this).parent().prev().prev().prev().prev().children().children("input").val();
				var this_tr = $(this).parent().parent();
				$.ajax({
					type: "get",
					url: "/shopping/delete",
					dataType: "JSON",
					data: {
						shopid:shopid
					},
					success: function (data) {
						if (data.code == 200) {
							this_tr.remove();
						} else {
							alert("删除失败");
						}
					}
				})
			})
		}
	})
}else{
	$("#shop1").addClass("d-none");
	$("#shop2").addClass("d-none")
	$("#shop3").removeClass("d-none");
}

$("#chbAll").click(function () {
	$("tbody>tr>td:first-child>div>input").prop("checked", true);
})

var table = document.getElementById("data");
table.onclick = function (e) {
	if (e.target.nodeName === "BUTTON") {
		//捷径: e.target->当前btn
		var btn = e.target;
		//3. 查找要修改的元素
		var input = btn.parentNode.children[1];
		//4. 修改元素
		var n = parseInt(input.value);
		if (btn == input.nextElementSibling)
			n++;
		else if (n > 1)
			n--;
		input.value = n;

		//3. 查找要修改的元素
		var td = btn.parentNode.parentNode.nextElementSibling;
		//4. 修改元素
		var price = parseFloat(
			btn.parentNode.parentNode //td
			.previousElementSibling //前一个td
			.innerHTML //¥4488.00
			.slice(1) //4488.00
		);
		var sub = price * n;
		td.innerHTML = `¥${sub.toFixed(2)}`;

		//3. 查找要修改的元素
		var tdTotal = zp_data.querySelector(
			"tr:first-child>td:last-child>h5"
		);
		//4. 修改元素
		var tds = table.querySelectorAll(
			"tbody>tr>td:nth-child(4)"
		);
		var total = 0;
		for (var td of tds) {
			total += parseFloat(td.innerHTML.slice(1))
		}
		tdTotal.innerHTML = `¥${total.toFixed(2)}`;
		var shopid = btn.parentNode.parentNode //td
			.previousElementSibling.previousElementSibling.children[0].children[0].value;
		$.ajax({
			type: "post",
			url: "/shopping/upcount",
			dataType: "JSON",
			data: {
				shopid: shopid,
				count: n
			},
			success: function (data) {

			}
		})
	}
	//当单击按钮时: btn.onclick() //this->当前btn
}

/*del_shop.onclick = function (e){
	console.log(this)
}*/
